home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP3.TXT < prev    next >
Text File  |  1987-11-21  |  19KB  |  455 lines

  1.  
  2.                         Chapter 3 - Program Control
  3.  
  4.  
  5.                                THE WHILE LOOP
  6.  
  7.              The  C programming language has several structures  for
  8.         looping  and conditional branching.   We will cover them all
  9.         in this chapter and we will begin with the while loop.   The
  10.         while  loop continues to loop while some condition is  true.
  11.         When   the   condition  becomes  false,   the   looping   is
  12.         discontinued.   It therefore does just what it says it does,
  13.         the name of the loop being very descriptive.
  14.  
  15.              Load the program WHILE.C and display it for an  example
  16.         of  a while loop.   We begin with a comment and the  program
  17.         name,  then  go  on  to define an integer  variable  "count"
  18.         within the body of the program.  The variable is set to zero
  19.         and we come to the while loop itself.  The syntax of a while
  20.         loop is just as shown here.  The keyword "while" is followed
  21.         by an expression of something in parentheses,  followed by a
  22.         compound  statement  bracketed by braces.   As long  as  the
  23.         expression in parenthesis is true, all statements within the
  24.         braces will be executed.   In this case,  since the variable
  25.         count  is incremented by one every time the  statements  are
  26.         executed, it will eventually reach 6, the statement will not
  27.         be executed,  and the loop will be terminated.   The program
  28.         control   will   resume  at  the  statement  following   the
  29.         statements in braces.
  30.  
  31.              We  will  cover  the compare  expression,  the  one  in
  32.         parentheses, in the next chapter.  Until then, simply accept
  33.         the  expressions for what you think they should do  and  you
  34.         will probably be correct.
  35.  
  36.              Several  things must be pointed out regarding the while
  37.         loop.   First,  if the variable count were initially set  to
  38.         any  number greater than 5,  the statements within the  loop
  39.         would  not be executed at all,  so it is possible to have  a
  40.         while  loop  that  never  is  executed.   Secondly,  if  the
  41.         variable  were  not incremented in the loop,  then  in  this
  42.         case,  the loop would never terminate, and the program would
  43.         never complete.   Finally, if there is only one statement to
  44.         be executed within the loop, it does not need braces but can
  45.         stand alone.
  46.  
  47.              Compile and run this program.
  48.  
  49.                              THE DO-WHILE LOOP
  50.  
  51.              A  variation  of the while loop is illustrated  in  the
  52.         program DOWHILE.C,  which you should load and display.  This
  53.         program  is nearly identical to the last one except that the
  54.         loop  begins  with the reserved word  "do",  followed  by  a
  55.         compound  statement  in  braces,   then  the  reserved  word
  56.  
  57.  
  58.                                  Page 11
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                         Chapter 3 - Program Control
  69.  
  70.  
  71.         "while",  and  finally  an expression in  parentheses.   The
  72.         statements in the braces are executed repeatedly as long  as
  73.         the expression in parentheses is true.   When the expression
  74.         in parentheses becomes false,  execution is terminated,  and
  75.         control passes to the statements following this statement.
  76.  
  77.              Several  things  must  be pointed  out  regarding  this
  78.         statement.  Since  the test is done at the end of the  loop,
  79.         the  statements  in  the braces will always be  executed  at
  80.         least once.   Secondly,  if "i" were not changed within  the
  81.         loop,  the loop would never terminate, and hence the program
  82.         would  never terminate.   Finally,  just like for the  while
  83.         loop,  if  only  one statement will be executed  within  the
  84.         loop,  no braces are required.  Compile and run this program
  85.         to see if it does what you think it should do.
  86.  
  87.              It  should come as no surprise to you that these  loops
  88.         can be nested.  That is, one loop can be included within the
  89.         compound  statement of another loop,  and the nesting  level
  90.         has no limit.
  91.  
  92.                                 THE FOR LOOP
  93.  
  94.              The  "for" loop is really nothing new,  it is simply  a
  95.         new  way  to describe the "while" loop.   Load and edit  the
  96.         file  named  FORLOOP.C for an example of a  program  with  a
  97.         "for"  loop.  The  "for" loop consists of the reserved  word
  98.         "for" followed by a rather large expression in  parentheses.
  99.         This expression is really composed of three fields separated
  100.         by  semi-colons.   The  first field contains the  expression
  101.         "index  = 0" and is an initializing field.   Any expressions
  102.         in  this field are executed prior to the first pass  through
  103.         the loop.   There is essentially no limit as to what can  go
  104.         here,  but  good programming practice would require it to be
  105.         kept simple.   Several initializing statements can be placed
  106.         in this field, separated by commas.
  107.  
  108.              The second field,  in this case containing "index < 6",
  109.         is  the  test which is done at the beginning  of  each  loop
  110.         through  the program.   It can be any expression which  will
  111.         evaluate  to a true or false.   (More will be said about the
  112.         actual value of true and false in the next chapter.)
  113.  
  114.              The expression contained in the third field is executed
  115.         each time the loop is executed but it is not executed  until
  116.         after  those  statements  in the main body of the  loop  are
  117.         executed.   This field, like the first, can also be composed
  118.         of several operations separated by commas.
  119.  
  120.              Following  the  for()  expression  is  any  single   or
  121.         compound statement which will be executed as the body of the
  122.  
  123.  
  124.                                  Page 12
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                         Chapter 3 - Program Control
  135.  
  136.  
  137.         loop.   A  compound  statement  is  any  group  of  valid  C
  138.         statements enclosed in braces.   In nearly any context in C,
  139.         a  simple statement can be replaced by a compound  statement
  140.         that will be treated as if it were a single statement as far
  141.         as program control goes.  Compile and run this program.
  142.  
  143.              You  may  be  wondering why there  are  two  statements
  144.         available that do exactly the same thing because the "while"
  145.         and  the "for" loop do exactly the same thing.  The  "while"
  146.         is convenient to use for a loop that you don't have any idea
  147.         how many times the loop will be executed, and the "for" loop
  148.         is  usually used in those cases when you are doing  a  fixed
  149.         number  of  iterations.  The "for" loop is  also  convenient
  150.         because  it moves all of the control information for a  loop
  151.         into one place, between the parentheses, rather than at both
  152.         ends  of the code.  It is your choice as to which you  would
  153.         rather use.
  154.  
  155.                               THE IF STATEMENT
  156.  
  157.              Load  and  display the file IFELSE.C for an example  of
  158.         our first conditional branching statement, the "if".  Notice
  159.         first,  that there is a "for" loop with a compound statement
  160.         as its executable part containing two "if" statements.  This
  161.         is an example of how statements can be nested.  It should be
  162.         clear  to  you  that each of the  "if"  statements  will  be
  163.         executed 10 times.
  164.  
  165.              Consider the first "if" statement.   It starts with the
  166.         keyword  "if" followed by an expression in parentheses.   If
  167.         the expression is evaluated and found to be true, the single
  168.         statement following the "if" is executed,  and if false, the
  169.         following  statement  is  skipped.   Here  too,  the  single
  170.         statement  can be replaced by a compound statement  composed
  171.         of  several statements bounded by  braces.   The  ex